﻿using System;
using System.Data.SQLite;

namespace MMVoiceComm
{
    internal class MusicDatabaseHandler
    {
        //Used for connection to MediaMonkeyDatabase
        public SQLiteConnection sqlConnector = new SQLiteConnection();


        ~MusicDatabaseHandler()
        {
            if(!sqlConnector.State.Equals(System.Data.ConnectionState.Closed)) sqlConnector.Close();
        }
        public void Connect()
        {
            //By default Open the Primary MediaMonkey Database
            string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) +
                          "\\MediaMonkey\\MM.DB";

            if(!sqlConnector.State.Equals(System.Data.ConnectionState.Closed)) sqlConnector.Close();
            sqlConnector.ConnectionString = "Data Source=" + path + ";Version=3;Read Only=True;";
            sqlConnector.Open();
        }

        public void Connect(String connectionString)
        {
            sqlConnector.ConnectionString = connectionString;
            sqlConnector.Open();
        }

        public SQLiteDataReader ExecuteSelect(String what, String where, String conditions)
        {
            SQLiteCommand sqlCmd = sqlConnector.CreateCommand();


            if (conditions.Length > 0)
            {
//Conditions is not empty
                sqlCmd.CommandText = "SELECT " + what + " FROM " + where + " WHERE " + conditions;
            }
            else
            {
                sqlCmd.CommandText = "SELECT " + what + " FROM " + where;
            }
            return sqlCmd.ExecuteReader();
        }


        public SQLiteDataReader ExecuteSelect(String what, String where)
        {
            return (ExecuteSelect(what, where, ""));
        }

        
    }
}